package controller

import (
	"gitee.com/Frank098/devops-app-mgt/model"
	"gitee.com/Frank098/devops-app-mgt/service"
	"github.com/gin-gonic/gin"
)

var Svc svc

type svc struct{}

// @Tags Service
// @Summary 查询Service详细信息
// @Accept json
// @Produce json
// @Param service_name query string true "Service名称"
// @Param namespace query string true "Service命名空间"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /svc/info [GET]
func (s *svc) GetServiceInfo(ctx *gin.Context) {
	var params model.GetServices
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Svc.GetServiceInfo(params.ServiceName, params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Service
// @Summary 创建Service
// @Accept json
// @Produce json
// @Param "" body model.ServiceCreate true "创建Service"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /svc/create [POST]
func (s *svc) CreateService(ctx *gin.Context) {
	var params model.ServiceCreate
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	if err := service.Svc.CreateService(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "svc创建成功",
	})
}

// @Tags Service
// @Summary 删除Service
// @Accept json
// @Produce json
// @Param "" body model.GetServices true "删除Service"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result ""
// @Router /svc/delete [DELETE]
func (s *svc) DeleteService(ctx *gin.Context) {
	var params model.GetServices
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	if err := service.Svc.DeleteService(params.ServiceName, params.Namespace); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: "svc删除成功",
	})
}

// @Tags Service
// @Summary 查询每个命名空间下Service信息
// @Accept json
// @Produce json
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /svc/all [GET]
func (s *svc) GetSvcAllPerNp(ctx *gin.Context) {
	data, err := service.Svc.GetSvcAllPerNp()
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Service
// @Summary 查询单个命名空间下Service信息
// @Accept json
// @Produce json
// @Param namespace query string true "Service命名空间"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /svc/single [GET]
func (s *svc) GetSvcSingleNp(ctx *gin.Context) {
	var params model.GetServices
	if err := ctx.Bind(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Svc.GetSvcSingleNp(params.Namespace)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

// @Tags Service
// @Summary 查询Service端口占用情况
// @Accept json
// @Produce json
// @Param "" body model.SvcPortQuery true "查询Service端口占用情况"
// @Success 200 {object} model.Result ""
// @Failure 400 {object} model.Result""
// @Router /svc/portquery [POST]
func (s *svc) SvcPortQuery(ctx *gin.Context) {
	var params model.SvcPortQuery
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	data, err := service.Svc.SvcPortQuery(params.Port)
	if err != nil {
		ErrorResponse(ctx, err, 400)
		return
	}
	SuccessResponse(ctx, &model.Result{
		Code: 200,
		Msg:  "Success",
		Data: data,
	})
}

/*
func (s *svc) UpdateService(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	err := service.Svc.UpdateService(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新Service成功",
		"data": nil,
	})
}

*/
